﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MySite.Model;
using System.Web.Security;
using MySite.BLL;

namespace MySite.Controllers
{
    public class AdminController : Controller
    {
        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Index()
        {
            ViewData["acname"] = this.Request.RequestContext.HttpContext.User.Identity.Name;

            //微博数
            DashBoardInfo dbi = new DashBoardInfo();

            if (!this.Request.RequestContext.HttpContext.Application.AllKeys.Contains("BeginTime"))
            {
                this.Request.RequestContext.HttpContext.Application.Add("BeginTime", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            }

            dbi.BeginTime = Convert.ToDateTime(this.Request.RequestContext.HttpContext.Application["BeginTime"]);

            dbi.StatusCount = new Status().Query("").Count;
            dbi.BlogCNCount = new Blog().Query("").Count;
            dbi.CnBetaCount = new Blog().Query("").Count;
            dbi.CommentCount = new Blog().Query("").Count;
            dbi.DailyRecordCount = new Blog().Query("").Count;
            dbi.DBSize = new Blog().Query("").Count;
            dbi.DownLoadCount = 0;

            TimeSpan ts = DateTime.Now.Subtract(dbi.BeginTime);
            dbi.RunTime = string.Format("{0}天{1}时{2}分", ts.Days,ts.Hours,ts.Minutes);
            dbi.VisitorCount = 0;
            dbi.VisitorSourceCount = 0;
            dbi.VoteCount = 0;
            return View(dbi);
        }
        #endregion

        #region 身份验证
        public ActionResult SignIn()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SignIn(string account, string password, string verifycode, string returnUrl)
        {
            if (MySite.Helper.Helper.VerifyCode(verifycode))
            {
                AccountInfo model = new Account().Query(string.Format(" Account_Name = '{0}'", account)).FirstOrDefault();

                if (model != null)
                {
                    if (string.Equals(model.Account_Password, MySite.Helper.Helper.HashString(password), StringComparison.InvariantCultureIgnoreCase))
                    {
                        CurrentHelper.CurrentUser = model;
                        FormsAuthentication.RedirectFromLoginPage(account, true);

                        if (!string.IsNullOrEmpty(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index");
                        }
                    }
                    else
                    {
                        return View();
                    }
                }
                else
                {
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

        //登出
        [Authorize]
        public ActionResult SignOut()
        {
            FormsAuthentication.SignOut();
            return View("Index");
        }
        #endregion

        #region 投票管理
        [Authorize]
        public ActionResult VoteList(int pageindex, int pagesize)
        {
            int pageCount = 0;
            IList<VoteInfo> viList = new Vote().Query("", " PID desc ", pageindex, 20, ref pageCount); ;
            return View(viList);
        }

        [Authorize]
        public ActionResult VoteAdd()
        {
            Dictionary<string, object> inputcss = new Dictionary<string, object>();
            inputcss.Add("class", "inputxt");
            ViewData["inputcss"] = inputcss;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult VoteAdd(VoteInfo vi)
        {
            vi.Author = CurrentHelper.CurrentUser.Account_Name;
            vi.CreateTime = DateTime.Now;
            vi.IsOver = false;
            vi.ID = new Vote().Query("").Count + 1;
            vi.VoteItems = new VoteItem().GeneralVoteItems(vi.ID, vi.Title, this.Request.Form["VoteItems"]);
            new Vote().UpdateVote(vi);
            new VoteItem().UpdateVote(vi.VoteItems);
            return RedirectToAction("VoteList", new { pageindex = 1, pagesize = 20 });
        }

        [Authorize]
        public ActionResult VoteEdit(string pid)
        {
            VoteInfo vi = new Vote().Query(string.Format("PID={0}", pid)).FirstOrDefault();
            vi.VoteItems = new VoteItem().Query(string.Format("VoteTitle='{0}'", vi.Title));
            IList<VoteItemInfo> viiList = vi.VoteItems;
            ViewData["viiList"] = viiList;
            return View(vi);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult VoteEdit(VoteInfo vi)
        {
            vi.VoteItems = new VoteItem().GeneralVoteItems(vi.ID, vi.Title, this.Request.Form["VoteItems"]);
            new Vote().UpdateVote(vi);
            new VoteItem().UpdateVote(vi.VoteItems);

            return RedirectToAction("VoteList", new { pageindex = 1, pagesize = 20 });
        } 
        #endregion

        #region 技能管理
        [Authorize]
        public ActionResult SkillList(int pageindex, int pagesize)
        {
            int pageCount = 0;
            IList<SkillInfo> siList = new Skill().Query("",20,pageindex," PID desc ", ref pageCount); ;
            return View(siList);
        }

        [Authorize]
        public ActionResult SkillAdd()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult SkillAdd(SkillInfo si)
        {
            new Skill().UpdateSkill(si);
            return RedirectToAction("SkillList", new { pageindex = 1, pagesize = 20 });
        }

        [Authorize]
        public ActionResult SkillEdit(string pid)
        {
            SkillInfo vi = new Skill().Query(string.Format("PID={0}", pid)).FirstOrDefault();
            return View(vi);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult SkillEdit(SkillInfo si)
        {
            si.Content = Server.HtmlEncode(si.Content);
            new Skill().UpdateSkill(si);
            return RedirectToAction("SkillList", new { pageindex = 1, pagesize = 20 });
        }
        #endregion

        #region  学习笔记管理
        [Authorize]
        public ActionResult StudyNoteList(int pageSize = 20, int pageIndex = 1)
        {
            IList<StudyNoteInfo> sniList = new StudyNote().Query("");
            return View(sniList);
        }
        [Authorize]
        public ActionResult StudyNoteEdit(int PID)
        {
            var selectitems = new ConfigItem().Query(string.Format("ConfigName='{0}'", "学习笔记分类")).Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue }).ToList();
            selectitems.Insert(0, new SelectListItem { Text = "", Value = "" });
            ViewData["sliList"] = selectitems;
            StudyNoteInfo sni = new StudyNote().Query(string.Format("PID={0}",PID),1).FirstOrDefault();
            return View(sni);
        }

        [Authorize]
        public ActionResult StudyNoteAdd()
        {
            var selectitems = new ConfigItem().Query(string.Format("ConfigName='{0}'", "学习笔记分类")).Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue }).ToList();
            selectitems.Insert(0, new SelectListItem { Text = "", Value = "" });
            ViewData["sliList"] = selectitems;
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult StudyNoteEdit(StudyNoteInfo sni)
        {
            sni.Content = Server.HtmlEncode(sni.Content);
            new StudyNote().UpdateStudyNotes(sni);
            return RedirectToAction("StudyNoteList", new { pageindex = 1, pagesize = 20 });
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult StudyNoteAdd(StudyNoteInfo sni)
        {
            sni.Content = Server.HtmlEncode(sni.Content);
            new StudyNote().UpdateStudyNotes(sni);
            return RedirectToAction("StudyNoteList", new { pageindex = 1, pagesize = 20 });
        }
        #endregion

        #region  配置项管理
        [Authorize]
        public ActionResult ConfigItemList(int pageSize = 20, int pageIndex = 1)
        {
            int pageCount = 0;
            IList<ConfigItemInfo> sniList = new ConfigItem().Query("", pageSize,"PID desc",ref pageCount);
            return View(sniList);
        }
        [Authorize]
        public ActionResult ConfigItemEdit(int PID)
        {
            ConfigItemInfo sni = new ConfigItem().Query(string.Format("PID={0}", PID), 1).FirstOrDefault();
            return View(sni);
        }

        [Authorize]
        public ActionResult ConfigItemAdd()
        {
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ConfigItemEdit(ConfigItemInfo sni)
        {
            new ConfigItem().UpdateConfigItem(sni);
            return RedirectToAction("ConfigItemList", new { pageindex = 1, pagesize = 20 });
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ConfigItemAdd(ConfigItemInfo sni)
        {
            new ConfigItem().UpdateConfigItem(sni);
            return RedirectToAction("ConfigItemList", new { pageindex = 1, pagesize = 20 });
        }

        public ActionResult ConfigItemDelete(int PID)
        {
            new ConfigItem().Delete(string.Format("PID={0}",PID));
            return RedirectToAction("ConfigItemList", new { pageindex = 1, pagesize = 20 });
        }
        #endregion

        #region 生活杂记
        /// <summary>
        /// 生活杂记
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult FunnyList(int pageSize = 20, int pageIndex = 1)
        {
            IList<FunnyInfo> fiList = new Funny().Query("");
            return View(fiList);
        }

        [Authorize]
        public ActionResult FunnyAdd()
        {
            ViewData["sliList"] = new ConfigItem().Query("ConfigName='生活杂记分类'").Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue }).ToList();
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult FunnyAdd(FunnyInfo fi)
        {
            new Funny().UpdateFunnys(fi);
            return RedirectToAction("FunnyList", new { pageindex = 1, pagesize = 20 });
        }

        public ActionResult FunnyEdit(int PID)
        {
            ViewData["sliList"] = new ConfigItem().Query("ConfigName='生活杂记分类'").Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue });
            FunnyInfo fi = new Funny().Query(string.Format("PID={0}", PID)).FirstOrDefault();
            return View(fi);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult FunnyEdit(FunnyInfo fi)
        {
            fi.UpdateDate = DateTime.Now;
            new Funny().UpdateFunnys(fi);
            return RedirectToAction("FunnyList", new { pageindex = 1, pagesize = 20 });
        }
        #endregion

        #region 我的作品
        /// <summary>
        /// 我的作品
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult ProductionList(int pageSize = 20, int pageIndex = 1)
        {

            IList<ProductionInfo> fiList = new Production().Query("");
            return View(fiList);
        }
        /// <summary>
        /// 添加我的作品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult ProductionAdd()
        {
            ViewData["sliList"] = new ConfigItem().Query("ConfigName='我的作品分类'").Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue }); 
            return View();
        }

        /// <summary>
        /// 添加我的作品
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ProductionAdd(ProductionInfo pi)
        {
            new Production().UpdateProductions(pi);
            return RedirectToAction("ProductionList", new { pageindex = 1, pagesize = 20 });
        }

        /// <summary>
        /// 编辑我的作品
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult ProductionEdit(int PID)
        {
            ViewData["sliList"] = new ConfigItem().Query("ConfigName='我的作品分类'").Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue }); 
            ProductionInfo pi = new Production().Query(string.Format("PID={0}", PID)).FirstOrDefault();
            return View(pi);
        }

        /// <summary>
        /// 编辑我的作品
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult ProductionEdit(ProductionInfo pi)
        {
            new Production().UpdateProductions(pi);
            return RedirectToAction("ProductionList", new { pageindex = 1, pagesize = 20 });
        }
        #endregion

        #region 我的作品
        /// <summary>
        /// 我的作品
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult AboutList(int pageSize = 20, int pageIndex = 1)
        {

            IList<AboutInfo> fiList = new About().Query("");
            return View(fiList);
        }
        /// <summary>
        /// 添加我的作品
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult AboutAdd()
        {
            ViewData["AboutTypeList"] = new ConfigItem().Query("ConfigName='关于我们分类'").Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue });
            return View();
        }

        /// <summary>
        /// 添加我的作品
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult AboutAdd(AboutInfo pi)
        {
            new About().UpdateAbouts(pi);
            return RedirectToAction("AboutList", new { pageindex = 1, pagesize = 20 });
        }

        /// <summary>
        /// 编辑我的作品
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult AboutEdit(int PID)
        {
            ViewData["AboutTypeList"] = new ConfigItem().Query("ConfigName='我的作品分类'").Select(p => new SelectListItem { Text = p.ConfigKey, Value = p.ConfigValue });
            AboutInfo pi = new About().Query(string.Format("PID={0}", PID)).FirstOrDefault();
            return View(pi);
        }

        /// <summary>
        /// 编辑我的作品
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult AboutEdit(AboutInfo pi)
        {
            new About().UpdateAbouts(pi);
            return RedirectToAction("AboutList", new { pageindex = 1, pagesize = 20 });
        }
        #endregion
    }
}
